package com.lancoo.znbkxx.teacher.widget

import android.content.Context
import android.util.AttributeSet
import com.scwang.smartrefresh.layout.internal.InternalAbstract
import com.scwang.smartrefresh.layout.constant.RefreshState
import com.scwang.smartrefresh.layout.api.RefreshLayout
import android.view.LayoutInflater
import android.view.View
import android.view.animation.*
import android.widget.ImageView
import android.widget.TextView
import com.lancoo.znbkxx.teacher.R


/**
 * @author : Admin
 * @date   : 2019/8/27 16:27
 * @desc   :
 */
class MyHeaderView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : InternalAbstract(context, attrs, defStyleAttr) {
    private val tvHint: TextView
    private val ivHeader: ImageView
    private val ivShadow: ImageView
    private val scaleHeader by lazy { ScaleAnimation(0.5f, 1.3f, 0.5f, 1.3f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f) }
    private val scaleShadow by lazy { ScaleAnimation(0.5f, 1.3f, 0.5f, 1.3f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f) }

    init {
//        val view = LayoutInflater.from(context).inflate(R.layout.view_header, this)
        val view = View.inflate(context,R.layout.view_header,this)
        tvHint = view.findViewById(R.id.tv_hint)
        ivHeader = view.findViewById(R.id.iv_header)
        ivShadow = view.findViewById(R.id.iv_shadow)
    }

    override fun onFinish(layout: RefreshLayout, success: Boolean): Int {
        ivHeader.clearAnimation()
        ivShadow.clearAnimation()
        if (success) {
            tvHint.text = REFRESH_HEADER_FINISH
        } else {
            tvHint.text = REFRESH_HEADER_FAILED
        }
        super.onFinish(layout, success)
        return 500 //延迟500毫秒之后再弹回
    }

    override fun onStateChanged(refreshLayout: RefreshLayout, oldState: RefreshState, newState: RefreshState) {
        when (newState) {
            RefreshState.PullDownToRefresh //下拉过程
            -> {
                tvHint.text = REFRESH_HEADER_PULLING
            }
            RefreshState.ReleaseToRefresh //松开刷新
            -> {
                tvHint.text = REFRESH_HEADER_RELEASE
                excuteAnimation()
            }
            RefreshState.Refreshing //loading中
            -> tvHint.text = REFRESH_HEADER_LOADING
        }
    }

    companion object {
//        var REFRESH_HEADER_PULLING = "下拉即可刷新"
//        var REFRESH_HEADER_LOADING = "加载中..."
//        var REFRESH_HEADER_RELEASE = "释放即可刷新"
//        var REFRESH_HEADER_FINISH = "刷新成功"
//        var REFRESH_HEADER_FAILED = "刷新失败"
        var REFRESH_HEADER_PULLING = ""
        var REFRESH_HEADER_LOADING = ""
        var REFRESH_HEADER_RELEASE = ""
        var REFRESH_HEADER_FINISH = ""
        var REFRESH_HEADER_FAILED = ""
    }

    private fun excuteAnimation() {
        scaleHeader.duration = 1000
        scaleHeader.repeatCount = Animation.INFINITE
        scaleHeader.repeatMode = Animation.REVERSE
        scaleShadow.duration = 1000
        scaleShadow.repeatCount = Animation.INFINITE
        scaleShadow.repeatMode = Animation.REVERSE
        ivHeader.startAnimation(scaleHeader)
        ivShadow.startAnimation(scaleShadow)
    }
}